#!/usr/bin/python

import shutil
import os

shutil.copy("/etc/sysconfig/network-scripts/ifcfg-eno16777736",
            "/etc/sysconfig/network-scripts/ifcfg-eno16777736.bak")

ip_addr = input("please input ip address:")
net_mask = input("please input netmask:")
gate_way = input("please input gateway:")
dns1_ip = input("please input dns1:")

f = open("/etc/sysconfig/network-scripts/ifcfg-eno16777736", "w")
f.write("DEVICE=eno16777736\nONBOOT=yes\nBOOTPROTO=none\nIPADDR=%s" % ip_addr)
f.write("\nNETWASK=%s" % net_mask)
f.write("\nGATEWAY=%s" % gate_way)
f.write("\nDNS1=%s" % dns1_ip)
f.close()

os.system("service network restart")

if os.system("ping -c2 %s" % ip_addr) == 0:
    print("ip: %s" % ip_addr)
    print("gateway: %s" % gate_way)
    print("netmask: %s" % net_mask)
    print("dns server: %s" % dns1_ip)
else:
    shutil.copy("/etc/sysconfig/network-scripts/ifcfg-eno16777736.bak",
                "/etc/sysconfig/network-scripts/ifcfg-eno16777736")
    print("config ip failed")
    os.system("service network restart")
